// tests/test-task.js
import axios from 'axios';

async function testTaskAPI() {
  const baseURL = 'http://localhost:3000';
  let authToken = '';
  let taskId = null;
  
  try {
    // 先登录获取token
    console.log('Logging in to get auth token...');
    const loginResponse = await axios.post(`${baseURL}/api/v1/login`, {
      username: 'testuser',
      password: '123456'
    });
    
    authToken = loginResponse.data.data.token;
    console.log('Auth token obtained');
    
    // 设置认证头部
    const authHeaders = {
      'Authorization': `Bearer ${authToken}`
    };
    
    // 测试创建任务
    console.log('\nTesting create task...');
    const createResponse = await axios.post(`${baseURL}/api/v1/tasks`, 
      { 
        group_id: 1,
        title: '测试任务',
        description: '这是一个测试任务',
        is_important: true,
        is_my_day: true,
        due_date: '2023-12-31'
      },
      { headers: authHeaders }
    );
    console.log('Create task response:', createResponse.data);
    
    if (createResponse.data.success && createResponse.data.data.task_id) {
      taskId = createResponse.data.data.task_id;
      
      // 测试获取任务列表
      console.log('\nTesting get tasks...');
      const getTasksResponse = await axios.get(`${baseURL}/api/v1/tasks`, 
        { headers: authHeaders }
      );
      console.log('Get tasks response:', getTasksResponse.data);
      
      // 测试获取"我的一天"任务
      console.log('\nTesting get my day tasks...');
      const getMyDayTasksResponse = await axios.get(`${baseURL}/api/v1/tasks/my-day`, 
        { headers: authHeaders }
      );
      console.log('Get my day tasks response:', getMyDayTasksResponse.data);
      
      // 测试获取任务详情
      console.log(`\nTesting get task detail (ID: ${taskId})...`);
      const getTaskDetailResponse = await axios.get(`${baseURL}/api/v1/tasks/${taskId}`, 
        { headers: authHeaders }
      );
      console.log('Get task detail response:', getTaskDetailResponse.data);
      
      // 测试更新任务
      console.log(`\nTesting update task (ID: ${taskId})...`);
      const updateTaskResponse = await axios.put(`${baseURL}/api/v1/tasks/${taskId}`, 
        { 
          title: '更新后的测试任务',
          status: 1
        },
        { headers: authHeaders }
      );
      console.log('Update task response:', updateTaskResponse.data);
      
      // 测试更新任务分组
      console.log(`\nTesting update task group (ID: ${taskId})...`);
      const updateTaskGroupResponse = await axios.put(`${baseURL}/api/v1/tasks/${taskId}/group`, 
        { 
          group_id: 2
        },
        { headers: authHeaders }
      );
      console.log('Update task group response:', updateTaskGroupResponse.data);
      
      // 测试添加任务日志
      console.log(`\nTesting add task log (ID: ${taskId})...`);
      const addTaskLogResponse = await axios.post(`${baseURL}/api/v1/tasks/${taskId}/logs`, 
        { 
          content: '这是任务日志内容'
        },
        { headers: authHeaders }
      );
      console.log('Add task log response:', addTaskLogResponse.data);
      
      // 测试获取任务日志
      console.log(`\nTesting get task logs (ID: ${taskId})...`);
      const getTaskLogsResponse = await axios.get(`${baseURL}/api/v1/tasks/${taskId}/logs`, 
        { headers: authHeaders }
      );
      console.log('Get task logs response:', getTaskLogsResponse.data);
      
      // 测试删除任务
      console.log(`\nTesting delete task (ID: ${taskId})...`);
      const deleteTaskResponse = await axios.delete(`${baseURL}/api/v1/tasks/${taskId}`, 
        { headers: authHeaders }
      );
      console.log('Delete task response:', deleteTaskResponse.data);
    }
  } catch (error) {
    console.error('Task API Test Error:', error.response ? error.response.data : error.message);
  }
}

testTaskAPI();